<?php
/**
 * Copyright (c) 2006, PORTALIER Julien
 * 
 * Licensed under The LGPL License
 * Redistributions of files must retain the above copyright notice.
 * 
 * @copyright    Copyright (c) 2006, Julien PORTALIER
 * @link         http://code.google.com/p/feather-cm
 * @license      http://www.opensource.org/licenses/lgpl-license.php The LGPL License
 */

class HtmlPagesController extends AppController
{
	public $restrictActions = array('index', 'create', 'edit', 'rename', 'delete');
	public $components      = array('Locker');
	
	function beforeAction()
	{
		// rewritting all params as a unique one (the full id)
		// Eg: /html_pages/edit/faq/authors -> id = /faq/authors 
		if (!empty($this->params['args']))
			$this->params['args'] = array(implode('/', $this->params['args']));
		
		parent::beforeAction();
	}
	
	// read
	
	function index()
	{
		$this->data = $this->HtmlPage->getList();
		$this->pageTitle = _("List of pages");
	}
	
	function show($id)
	{
		$this->data = $this->HtmlPage->read();
		
		if (!empty($this->data))
			$this->pageTitle = $this->data['HtmlPage']['title'];
		elseif (!$this->Acl->check('html_pages.edit'))
			Error::show(404);
		else
		{
			$this->set('html_page_id', $id);
			$this->render('undefined');
		}
	}
	
	// create & update
	
	function create($id)
	{
		$this->Locker->lock("HtmlPage.$id");
		
		if (empty($this->data))
		{
			$this->data = $this->HtmlPage->getDefault($id);
		}
		elseif ($this->HtmlPage->save(null, true, 'insert'))
		{
			$this->Locker->unlock();
			$this->flash(_('Page created.'), "/$id", 'notice');
		}
		
		$this->pageTitle = sprintf(_("Creation of page /%s"), $id);
	}
	
	function edit($id)
	{
		$this->Locker->lock("HtmlPage.$id");
		
		if (empty($this->data)) {
			$this->data = $this->HtmlPage->read();
		}
		elseif ($this->HtmlPage->save())
		{
			$this->Locker->unlock();
			$this->flash(_('Page saved.'), "/html/$id", 'notice');
		}
		
		$this->pageTitle = sprintf(_("Edition of page /%s"), $id);
	}
	
	function rename($id)
	{
		// does page $id exists?
		$check = $this->HtmlPage->read('id');
		if (empty($check))
			return $this->flash(sprintf(_('Error: no such page /%s.'), $id), HTTP::getReferer());
		
		if (!isset($this->data['HtmlPage']['id'])) {
			$this->data = array('HtmlPage' => array('id' => $id));
		}
		else
		{
			// tries to rename page
			$ret = $this->HtmlPage->rename($this->data['HtmlPage']['id']);
			if ($ret === true)
			{
				$this->flash(sprintf(_("Page /%s renamed to /%s."),
					$id, $this->data['HtmlPage']['id']), HTTP::getReferer());
			}
			elseif(is_string($ret)) {
				$this->__error($ret);
			}
			$this->__error($ret);
		}
		$this->set('old_id', $id);
		$this->pageTitle = sprintf(_('Rename page /%s.'), $id);
	}
	
	function cancel($id)
	{
		$this->Locker->unlock("HtmlPage.$id");
		HTTP::redirect("/$id");
	}
	
	// delete
	
	function delete($id)
	{
		if ($this->HtmlPage->delete($id))
			$this->flash(sprintf(_('Page /%s deleted.'), $id), '/', 'notice');
		$this->__error();
	}
	
	protected function __error($mesg=null)
	{
		$this->flash(empty($mesg) ? _('An error occured.') : $mesg, HTTP::getReferer(), 'error');
	}
}
?>